package com.qg.wcsopcuaqj.dispatch.controller;


import com.qg.wcsopcuaqj.business.entity.TWcsRgvAlarm;
import com.qg.wcsopcuaqj.business.entity.TWcsTask;
import com.qg.wcsopcuaqj.business.entity.TWcsTaskLog;
import com.qg.wcsopcuaqj.business.service.ITWcsRgvAlarmService;
import com.qg.wcsopcuaqj.business.service.ITWcsTaskLogService;

import com.qg.wcsopcuaqj.business.service.ITWcsTaskService;
import com.qg.wcsopcuaqj.common.utils.CommonFunction;
import com.qg.wcsopcuaqj.dispatch.constant.QGSConstant;
import com.qg.wcsopcuaqj.dispatch.entity.RGV1Entity;
import com.qg.wcsopcuaqj.dispatch.entity.RGVMonitor;
import com.qg.wcsopcuaqj.dispatch.service.DDJService;
import com.qg.wcsopcuaqj.enums.rgv1.RGV1Plc2WcsVarEnum;
import com.qg.wcsopcuaqj.enums.rgv1.RGV1Wcs2PlcVarEnum;
import com.qg.wcsopcuaqj.enums.rgv2.RGV2Plc2WcsVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya1.YY1Plc2WcsVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya1.YY1Wcs2PlcVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya2.YY2Plc2WcsVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya2.YY2Wcs2PlcVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya3.YY3Plc2WcsVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya3.YY3Wcs2PlcVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya4.YY4Plc2WcsVarEnum;
import com.qg.wcsopcuaqj.enums.yeyatai.yeya4.YY4Wcs2PlcVarEnum;
import com.qg.wcsopcuaqj.opcua.controller.R;
import com.qg.wcsopcuaqj.opcua.selfunion.Enum.PLCType;
import com.qg.wcsopcuaqj.opcua.selfunion.UAService;
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 蔡翔
 * @Date: 2019/10/12 15:15
 * @Version 1.0
 */
@RestController
@RequestMapping("/rgv1")
public class WcsControllerOfRgv1 {
    @Autowired
    DDJService service;

    @Autowired
    UAService uaService;

    @Autowired
    ITWcsTaskService taskService;

    @Autowired
    ITWcsTaskLogService logService;


    @Autowired
    ITWcsRgvAlarmService alarmService;

    //todo 检查 test类 枚举类  controller类 server。impl runnable 类
    @PostMapping("/sendTask")
    public R sendTask(@RequestBody RGV1Entity rgv1Entity) {
        //1.先录到 任务表、日志表
        //①
        TWcsTask tWcsTask = new TWcsTask();
        tWcsTask.setCreateTime(LocalDateTime.now());
        tWcsTask.setValid(1);
        tWcsTask.setUpdateTime(LocalDateTime.now());
        tWcsTask.setCode(rgv1Entity.getTaskNo().toString());
        //wms 发给我的都是正在执行，在排队中的任务留在wms中先别给我
        tWcsTask.setStatus(1);
        //int all = taskMapper.insertSelective(tWcsTask);
        taskService.save(tWcsTask);

        //②
        TWcsTaskLog log = new TWcsTaskLog();
        log.setValid(1);
        log.setCreateTime(LocalDateTime.now());
        log.setContent("任务编号："+rgv1Entity.getTaskNo().toString());
        log.setSendStatus("否");
        log.setReceiveStatus("是");
        log.setSource("wms");
        log.setUpdateTime(LocalDateTime.now());
        logService.save(log);

        //发起堆垛机任务
        Integer integer = service.rgv1Task(rgv1Entity);

        //2.修改 任务表、日志表中的记录
        TWcsTask tWcsTaskUpdate = new TWcsTask();
        tWcsTaskUpdate.setStatus(integer);
        tWcsTaskUpdate.setId(tWcsTask.getId());
        taskService.update(tWcsTaskUpdate);

        TWcsTaskLog logUpdate = new TWcsTaskLog();
        logUpdate.setId(log.getId());
        logUpdate.setSendStatus("是");
        logService.update(logUpdate);


        if(integer!=1){
            return R.error(integer.toString());
        }else {
            return R.ok(integer.toString());
        }
    }
    @PostMapping("/sendTaskForTest")
    public R sendTaskForTest(@RequestBody RGV1Entity rgv1Entity) {
        //1.先录到 任务表、日志表
        //①
        TWcsTask tWcsTask = new TWcsTask();
        tWcsTask.setCreateTime(LocalDateTime.now());
        tWcsTask.setValid(1);
        tWcsTask.setUpdateTime(LocalDateTime.now());
        tWcsTask.setCode(rgv1Entity.getTaskNo().toString());
        //wms 发给我的都是正在执行，在排队中的任务留在wms中先别给我
        tWcsTask.setStatus(1);
        //int all = taskMapper.insertSelective(tWcsTask);
        taskService.save(tWcsTask);

        //②
        TWcsTaskLog log = new TWcsTaskLog();
        log.setValid(1);
        log.setCreateTime(LocalDateTime.now());
        log.setContent("任务编号："+rgv1Entity.getTaskNo().toString());
        log.setSendStatus("否");
        log.setReceiveStatus("是");
        log.setSource("wms");
        log.setUpdateTime(LocalDateTime.now());
        //logService.create(log);
        logService.save(log);


        //2.修改 任务表、日志表中的记录
        TWcsTask tWcsTaskUpdate = new TWcsTask();
        tWcsTaskUpdate.setStatus(1);
        tWcsTaskUpdate.setId(tWcsTask.getId());
        taskService.update(tWcsTaskUpdate);

        TWcsTaskLog logUpdate = new TWcsTaskLog();
        logUpdate.setId(log.getId());
        logUpdate.setSendStatus("是");
        logService.update(logUpdate);


        if(1!=1){
            return R.error();
        }else {
            return R.ok();
        }
    }


    @PostMapping("/getRGV1Status")
    public R getRGV1Status() throws Exception {
        //1.先录到 任务表、日志表
//        //agv 是否空空闲
//        String agvIsFree = getValue(RGV1Plc2WcsVarEnum.Free);
//        //agv 是否在线
//        String agvIsOnline = getValue(RGV1Plc2WcsVarEnum.Online);
//        String agvIsAlarm = getValue(RGV1Plc2WcsVarEnum.Alarm);
//        //agv 载货台是否有货
//        String agvHaveGoods = getValue(RGV1Plc2WcsVarEnum.Goods);
//        //任务号
//        String taskNo = getValue(RGV1Plc2WcsVarEnum.TaskNo);
//        String alarmInfoArray = getValueBoolArray(RGV1Plc2WcsVarEnum.AlarmInfo1);


        //agv 是否空空闲
        String agvIsFree = "true";
        //agv 是否在线
        String agvIsOnline = "true";
        String agvIsAlarm = "true";
        //agv 载货台是否有货
        String agvHaveGoods = "true";
        //任务号
        String taskNo = "";
        String alarmInfoArray = "[true,true,true,true]";
        RGVMonitor rgvMonitor = new RGVMonitor(agvIsFree, agvIsOnline, agvIsAlarm, agvHaveGoods, taskNo, alarmInfoArray);
        return R.ok().put("RGV1Status",rgvMonitor);
    }






    @PostMapping("/testWrites")
    public void testWrites() throws Exception {
        Boolean b = setValue(RGV1Wcs2PlcVarEnum.STB,RGV1Wcs2PlcVarEnum.STB.getType(),1);
        System.out.println(b);
        System.out.println();
    }

    //test for ddj read and write
    @PostMapping("/testForReadForRGV1")
    public void testForReadForRGV1() throws Exception {
        for(RGV1Plc2WcsVarEnum var: RGV1Plc2WcsVarEnum.values()){
            System.out.println(
                    var.getName() +" valueIs : "+
                            Boolean.valueOf(getValue(var))+ "type : "+
                            CommonFunction.judgeVarType(
                                    getValues(var).getValue().getValue()
                            )
            );
        }
        System.out.println("==============================================================");
        //boolean
        for(RGV1Wcs2PlcVarEnum var: RGV1Wcs2PlcVarEnum.values()){
            System.out.println(
                    var.getName() +" valueIs : "+
                            Boolean.valueOf(getValue(var))+ "type : "+
                            CommonFunction.judgeVarType(
                                    getValues(var).getValue().getValue()
                            )
            );
        }
    }


    @PostMapping("/testForReadForyeya1")
    public void testForReadForyeya1() throws Exception {
        for(YY1Plc2WcsVarEnum var: YY1Plc2WcsVarEnum.values()){
            System.out.println(
                    var.getName() +" valueIs : "+
                            Boolean.valueOf(getValue(var))+ "type : "+
                            CommonFunction.judgeVarType(
                                    getValues(var).getValue().getValue()
                            )
            );
        }
        System.out.println("==============================================================");
        //boolean
        for(YY1Wcs2PlcVarEnum var: YY1Wcs2PlcVarEnum.values()){
            System.out.println(
                    var.getName() +" valueIs : "+
                            Boolean.valueOf(getValue(var))+ "type : "+
                            CommonFunction.judgeVarType(
                                    getValues(var).getValue().getValue()
                            )
            );
        }
    }

    @PostMapping("/testForReadForyeya2")
    public void testForReadForyeya2() throws Exception {
        for(YY2Plc2WcsVarEnum var: YY2Plc2WcsVarEnum.values()){
            System.out.println(
                    var.getName() +" valueIs : "+
                            Boolean.valueOf(getValue(var))+ "type : "+
                            CommonFunction.judgeVarType(
                                    getValues(var).getValue().getValue()
                            )
            );
        }
        System.out.println("==============================================================");
        //boolean
        for(YY2Wcs2PlcVarEnum var: YY2Wcs2PlcVarEnum.values()){
            System.out.println(
                    var.getName() +" valueIs : "+
                            Boolean.valueOf(getValue(var))+ "type : "+
                            CommonFunction.judgeVarType(
                                    getValues(var).getValue().getValue()
                            )
            );
        }
    }
    @PostMapping("/testForReadForyeya3")
    public void testForReadForyeya3() throws Exception {
        for(YY3Plc2WcsVarEnum var: YY3Plc2WcsVarEnum.values()){
            System.out.println(
                    var.getName() +" valueIs : "+
                            Boolean.valueOf(getValue(var))+ "type : "+
                            CommonFunction.judgeVarType(
                                    getValues(var).getValue().getValue()
                            )
            );
        }
        System.out.println("==============================================================");
        //boolean
        for(YY3Wcs2PlcVarEnum var: YY3Wcs2PlcVarEnum.values()){
            System.out.println(
                    var.getName() +" valueIs : "+
                            Boolean.valueOf(getValue(var))+ "type : "+
                            CommonFunction.judgeVarType(
                                    getValues(var).getValue().getValue()
                            )
            );
        }
    }

    @PostMapping("/testForReadForyeya4")
    public void testForReadForyeya4() throws Exception {
        for(YY4Plc2WcsVarEnum var: YY4Plc2WcsVarEnum.values()){
            System.out.println(
                    var.getName() +" valueIs : "+
                            Boolean.valueOf(getValue(var))+ "type : "+
                            CommonFunction.judgeVarType(
                                    getValues(var).getValue().getValue()
                            )
            );
        }
        System.out.println("==============================================================");
        //boolean
        for(YY4Wcs2PlcVarEnum var: YY4Wcs2PlcVarEnum.values()){
            System.out.println(
                    var.getName() +" valueIs : "+
                            Boolean.valueOf(getValue(var))+ "type : "+
                            CommonFunction.judgeVarType(
                                    getValues(var).getValue().getValue()
                            )
            );
        }
    }
    //todo 先测试opc 的写
    @PostMapping("/testForWrite")
    public void testForWrite() throws Exception {
        //todo 明天写
        Boolean STB = setValue(RGV1Wcs2PlcVarEnum.STB, RGV1Wcs2PlcVarEnum.STB.getType(), 1);
        System.out.println("STB : "+STB);
        Boolean ACK = setValue(RGV1Wcs2PlcVarEnum.ACK, RGV1Wcs2PlcVarEnum.ACK.getType(), 1);
        System.out.println("ACK : "+ACK);


        Boolean pickRow = setValue(RGV1Wcs2PlcVarEnum.PickRow, RGV1Wcs2PlcVarEnum.PickRow.getType(), 1);
        System.out.println("pickRow : "+pickRow);
        Boolean pickBay = setValue(RGV1Wcs2PlcVarEnum.PickBay, RGV1Wcs2PlcVarEnum.PickBay.getType(), 1);
        System.out.println("pickBay : "+pickBay);

        Boolean sendBay = setValue(RGV1Wcs2PlcVarEnum.SendBay, RGV1Wcs2PlcVarEnum.SendBay.getType(), 1);
        System.out.println("sendBay : "+sendBay);
        Boolean sendRow = setValue(RGV1Wcs2PlcVarEnum.SendRow, RGV1Wcs2PlcVarEnum.SendRow.getType(), 1);
        System.out.println("sendRow : "+sendRow);

        Boolean taskNo = setValue(RGV1Wcs2PlcVarEnum.TaskNo, RGV1Wcs2PlcVarEnum.TaskNo.getType(), 1);
        System.out.println("taskNo : "+taskNo);
        Boolean taskType = setValue(RGV1Wcs2PlcVarEnum.TaskType, RGV1Wcs2PlcVarEnum.TaskType.getType(), 1);
        System.out.println("taskType : "+taskType);
    }

    private String getValue(RGV1Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private String getValue(RGV2Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }
    public String boolList(Boolean[] b){
        List<Boolean> c = new ArrayList<>();
        for(int i=0;i<b.length;i++){
            c.add(b[i]);
        }
        return c.toString();
    }
    private String getValueBoolArray(RGV1Plc2WcsVarEnum var) throws Exception {
        Boolean[] c = (Boolean[])uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue();
        return boolList(c);
    }
    private String getValueBoolArray(RGV2Plc2WcsVarEnum var) throws Exception {
        Boolean[] c = (Boolean[])uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue();
        return boolList(c);
    }

    private String getValue(RGV1Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private String getValue(YY1Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private String getValue(YY2Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }
    private String getValue(YY2Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private String getValue(YY3Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }
    private String getValue(YY3Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private String getValue(YY4Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }
    private String getValue(YY4Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private String getValue(YY1Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName).getValue().getValue().toString();
    }

    private DataValue getValues(RGV1Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }
    private DataValue getValues(RGV1Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }

    private DataValue getValues(YY1Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }
    private DataValue getValues(YY1Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }

    private DataValue getValues(YY2Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }
    private DataValue getValues(YY2Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }

    private DataValue getValues(YY3Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }
    private DataValue getValues(YY3Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }

    private DataValue getValues(YY4Plc2WcsVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }
    private DataValue getValues(YY4Wcs2PlcVarEnum var) throws Exception {
        return uaService.getValue(var.getNamespace(), var.getIdentity(), QGSConstant.plcName);
    }

    private Boolean setValue(RGV1Wcs2PlcVarEnum var, String dataType, Object newValue) throws Exception {
        return uaService.setValue(
                var.getNamespace(),
                var.getIdentity(),
                PLCType.valueOf(dataType).convertType(newValue),
                QGSConstant.plcName);
    }
    private Boolean setValue(RGV1Plc2WcsVarEnum var, String dataType, Object newValue) throws Exception {
        return uaService.setValue(
                var.getNamespace(),
                var.getIdentity(),
                PLCType.valueOf(dataType).convertType(newValue),
                QGSConstant.plcName
        );
    }
}
